package com.alibaba.cirtus.ots.application.app.ability.facade.v2serviceimpl;

import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.bcorder.model.dto.PageQuerySalePlanListRequest;
import com.epoch.app.bcorder.model.dto.PageQuerySalePlanListResponse;
import com.epoch.app.bcorder.service.SalesPlanService;
import com.epoch.app.otsapplicationappsdk.todolist.dto.AsyncRequest;
import com.epoch.app.otsapplicationappsdk.todolist.dto.RenderRequest;
import com.epoch.app.otsapplicationappsdk.todolist.service.TodoListService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class TodoListV2ServiceImpl implements TodoListService {

    private static Log log = Log.getLogger(TodoListV2ServiceImpl.class);

    @Autowired
    private SalesPlanService salesPlanService;

    @Override
    public Object render(RenderRequest renderRequest) {
        log.info("待办任务列表渲染页面请求参数 {} ", JSONObject.toJSONString(renderRequest));

        JSONObject result = new JSONObject();
        try {
            PageQuerySalePlanListRequest planListRequest = new PageQuerySalePlanListRequest();
            BeanUtils.copyProperties(renderRequest, planListRequest);
            int limit = 10;
            if (renderRequest.getSize() != null && renderRequest.getSize() > 0) {
                limit = renderRequest.getSize();
            }
            if (renderRequest.getPage() == null) {
                renderRequest.setPage(1);
            }
            int start = (renderRequest.getPage() - 1) * limit;
            planListRequest.setLimit(limit);
            planListRequest.setStart(start);
            try {
                PageQuerySalePlanListResponse planListResponse = salesPlanService.pageQuerySalePlanList(planListRequest);
                log.info("审批列表{}", JSON.toJSONString(planListResponse));
                result.put("planListResponse", planListResponse.getResult());
                int totalPage = planListResponse.getTotal() / limit;
                if (planListResponse.getTotal() % limit > 0) {
                    totalPage++;
                }
                result.put("totalPage", totalPage);
            } catch (Exception e) {
                result.put("totalPage", 0);
                log.error("审批列表异常{}", e);
            }
            result.put("success", true);
            result.put("message", "");

        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "页面跑丢了");
        }
        return result;
    }

    @Override
    public Object async(AsyncRequest asyncRequest) {
        log.info("待办任务列表页面异步请求参数 {} ", JSONObject.toJSONString(asyncRequest));
        Object request = asyncRequest.getRequest();
        if (request == null) {
            return null;
        }
        JSONObject result = new JSONObject();
        try {
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(request));
            JSONObject fields = jsonObject.getJSONObject("fields");
            Integer currentPage = fields.getInteger("currentPage");
            Integer pageSize = fields.getInteger("pageSize");
            int limit = 10;
            if (pageSize != null && pageSize > 0) {
                limit = pageSize;
            }
            if (currentPage == null) {
                currentPage = 1;
            }
            int start = (currentPage - 1) * limit;
            PageQuerySalePlanListRequest planListRequest = new PageQuerySalePlanListRequest();
            planListRequest.setStart(start);
            planListRequest.setLimit(limit);
            try {
                PageQuerySalePlanListResponse planListResponse = salesPlanService.pageQuerySalePlanList(planListRequest);
                log.info("审批列表{}", JSON.toJSONString(planListResponse));
                result.put("planListResponse", planListResponse.getResult());
                result.put("pageSize", limit);
                result.put("currentPage", currentPage);
                result.put("total", planListResponse.getTotal());
                int totalPage = planListResponse.getTotal() / limit;
                if (planListResponse.getTotal() % limit > 0) {
                    totalPage++;
                }
                result.put("totalPage", totalPage);
            } catch (Exception e) {
                result.put("totalPage", 0);
                log.error("审批列表异常{}", e);
            }
            result.put("success", true);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "页面跑丢了");
        }
        return result;
    }

}

